package com.zx.role.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zx.role.domain.SysUser;
import com.zx.role.execption.MyExecption;
import com.zx.role.mapper.SysUserMapper;
import com.zx.role.service.ISysUserService;
import com.zx.role.service.IUserService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * ROPAGATION_REQUIRES_NEW
 * 创建一个新的事务，如果当前存在事务，则把当前事务挂起
 */
@Service("iUserService")
public class IUserServiceImpl extends ServiceImpl<SysUserMapper,SysUser> implements IUserService {

    @Autowired
    private IUserService iUserService;

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public void insertObject(SysUser sysUser) throws MyExecption {
        if(TransactionSynchronizationManager.isActualTransactionActive()) {
            System.out.println("iUserService2@insertObjcet事务的唯一标识是：" + TransactionSynchronizationManager.getCurrentTransactionName());
        }
            System.out.println("插入之前UserId:"+sysUser.getUserId());
            baseMapper.insert(sysUser);
            System.out.println("插入之后UserId:"+sysUser.getUserId());
            System.out.println("xgh2222");
            iUserService.updateObject(sysUser);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateObject(SysUser sysUser) throws MyExecption {
        if(TransactionSynchronizationManager.isActualTransactionActive()){
            System.out.println("iUserService2@insertObjcet事务的唯一标识是："+TransactionSynchronizationManager.getCurrentTransactionName());
        }
        baseMapper.updateById(sysUser);
        throw new MyExecption("更新失败了");
    }
}
